package web;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.User;

import service.UserService;
import util.web.ServletUtil;

public class LoginUser extends HttpServlet {

	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletUtil.forware(request, response, "login");
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		try {
			User user = new UserService().findUser(username,password,request);
			
			
			
			ServletUtil.setsessionAttri(request, "user", user);
			String tourl = (String) request.getSession().getAttribute("tourl");
			if (tourl != null) {
				ServletUtil.sendRedirect(response, tourl);
				return;
			}
			ServletUtil.sendRedirect(response, "index.jspx");
		} catch (Exception e) {
			ServletUtil.setsessionAttri(request, "loginerror", e);
			ServletUtil.forware(request, response, "login");
		}
		
		
			
	}

	
	
	
	
	
	
	
	
}
